package com.woniu.product.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.product.pojo.Product;
import com.woniu.product.pojo.ProductComment;
import com.woniu.product.service.IProductCommentService;
import com.woniu.product.entity.dto.DisplayProductByConditionDTO;
import com.woniu.product.entity.dto.ProductSaleSortByUserDTO;
import com.woniu.product.pojo.Product;
import com.woniu.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author linxin
 * @since 2023-02-16
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;


    @ApiOperation("根据ID查询商品")
    @GetMapping("/queryById/{id}")
    public Result<Product> queryById(@PathVariable Integer id){
        Product product = productService.getById(id);
        return Result.ok(product);
    }


    @ApiOperation("查询所有商品")
    @GetMapping("/queryAll")
    public Result<List<Product>> queryAll(){
        List<Product> productList = productService.list();
        return Result.ok(productList);
    }

    // 搜索（根据商品的名称做一个模糊查询）+分页
    @ApiOperation("搜索+分页lin")
    @PostMapping("/list/querySearch/{currentPage}/{pageSize}")
    public Result<PageResult<Product>> querySearch(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            String productName){
        PageResult<Product> pageResult = productService.querySearch(currentPage, pageSize, productName);
        return Result.ok(pageResult);
    }

    @ApiOperation("新增或者修改商品")
    @PutMapping("/saveOrUpdate")
    public Result<Product> querySaveProduct(@RequestBody Product product){
        boolean pro = productService.saveOrUpdate(product);
        return Result.ok();
    }


    @ApiOperation("根据id删除商品")
    @DeleteMapping("/{id}")
    public Result<ProductComment> queryDeleteProduct(@PathVariable Integer id){
        boolean product = productService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("商品上下架")
    @PutMapping("/{id}")
    public Result queryStatus(@PathVariable Long id){

        boolean b = productService.queryProductStatus(id);

        return Result.ok();
    }

    @GetMapping("/find/categoryId")
    @ApiOperation("根据分类查询商品")
    public List<Product> findByCategoryId(@RequestParam String productCategoryId){
        return productService.findByCategoryId(productCategoryId);
    }

    @GetMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("搜索+分页")
    public Result<PageResult<Product>> searchProduct(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            String productName
    ){
        PageResult<Product> pageResult = productService.searchProduct(currentPage,pageSize,productName);
        return Result.ok(pageResult);
    }

    @PostMapping("/list/search/{currentPage}/{pageSize}")
    @ApiOperation("搜索+分页+按销量排序")
    public Result<PageResult<Product>> searchProductSortBySale(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody ProductSaleSortByUserDTO productSaleSortByUserDTO
            ){
        PageResult<Product> pageResult = productService.searchProductSort(currentPage,pageSize,productSaleSortByUserDTO);
        return Result.ok(pageResult);
    }

    @PostMapping("/condition/search/{currentPage}/{pageSize}")
    @ApiOperation("搜索+分页+按价格区间显示")
    public Result<PageResult<Product>> displayProductByCondition(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody DisplayProductByConditionDTO displayProductByConditionDTO
            ){
        PageResult<Product> pageResult = productService.searchProductSortByCondition(currentPage,pageSize,displayProductByConditionDTO);
        return Result.ok(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据商品ID查询商品信息")
    public Result<Product> selectProductById(@PathVariable String id){
        Product product = productService.selectProductById(id);
        return Result.ok(product);
    }

    @GetMapping("/{id}/{productQuantity}")
    @ApiOperation("修改库存")
    public Result updateStock(@PathVariable String id, @PathVariable Integer productQuantity){
        int row = productService.updateStock(id,productQuantity);
        return Result.ok(row);
    }
}
